package com.spyx.cart.controller;

import com.atyx.spyx.model.order.CartInfo;
import com.atyx.spyx.vo.order.OrderConfirmVo;
import com.spyx.activity.ActivityClient;
import com.spyx.auth.AuthContextHolder;
import com.spyx.cart.service.CartService;
import com.spyx.result.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("api/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    //添加购物车   skuNum 购买数量
    @GetMapping("addToCart/{skuid}/{skuNum}")
    public Result addToCart(
            @PathVariable("skuid") Long skuid,
            @PathVariable("skuNum")Long skuNum
    ){
        Long userId = AuthContextHolder.getUserId();
        cartService.addtoCart(skuid,skuNum,userId);
        return Result.ok();
    }

    //删除购物车  1.单个删除购物车中的商品  2.批量删除购物车的商品  3.清空购物车
    @DeleteMapping("deleteCart/{skuid}")
    public Result deleteById(
            @PathVariable("skuid")Long skuid
    ){
        Long userId = AuthContextHolder.getUserId();
        cartService.deleteByskuId(skuid,userId);
        return Result.ok();
    }

    //批量删除购物车的商品 skuid=[1,3,5,6,7]
    @PostMapping("batchDeleteCart")
    public Result deleteByIds(@RequestBody List<Long> skuids){
        Long userId = AuthContextHolder.getUserId();
        cartService.deleteByskuIds(userId,skuids);
        return Result.ok();
    }

    //清空购物车
    @DeleteMapping("deleteAllCart")
    public Result clearCart(){
        Long userId = AuthContextHolder.getUserId();
        cartService.clearCart(userId);
        return Result.ok();
    }

    //查询购物车列表
    @GetMapping("cartList")
    public Result cartList(){
       Long userId = AuthContextHolder.getUserId();
       List<CartInfo> cartList= cartService.getCartList(userId);
        return Result.ok(cartList);
    }


    @Autowired
    private ActivityClient activityClient;
    //带优惠活动的购物车列表查看
    @GetMapping("/activityCartList")
    public Result activityCartList(){

        //1.获取用户id,根据用户id,查看购物车列表
        Long userId = AuthContextHolder.getUserId();
        List<CartInfo> cartInfoList = cartService.getCartList(userId);
        //避免用户未添加购物车，就直接点查看购物车
        if(CollectionUtils.isEmpty(cartInfoList)){
            return Result.ok();
        }
        //2.根据用户id和购物车列表信息，查看商品参加的优惠活动和优惠券- service-activity
        OrderConfirmVo confirmVo = activityClient.findCartActivityAndCoupon(cartInfoList, userId);

        return Result.ok(confirmVo);
    }

    //更新购物车商品的选中状态:
    //1.单个更新    2.批量更新   3.全部更新(全选/全不选)
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result updateStatus(
            @PathVariable("skuId")Long skuId,
            @PathVariable("isChecked")Long isChecked
    ){
        //1.先获取用户ID
        Long userId = AuthContextHolder.getUserId();
        cartService.checkCart(skuId,isChecked,userId);
        return Result.ok();
    }


    //2.全部更新选中状态
    @GetMapping("checkAllCart/{isChecked}")
    public Result checkAllCart(@PathVariable(value = "isChecked") Integer isChecked) {
        // 1.获取用户Id
        Long userId = AuthContextHolder.getUserId();
        // 2.调用更新方法
        cartService.checkAllCart(userId, isChecked);
        return Result.ok();
    }

    //3.批量更新选中状态 [1,3,5]
    @ApiOperation(value="批量选择购物车")
    @PostMapping("batchCheckCart/{isChecked}")
    public Result batchCheckCart(@RequestBody List<Long> skuIdList,
                                 @PathVariable(value = "isChecked") Integer isChecked){
        // 如何获取userId
        Long userId = AuthContextHolder.getUserId();
        cartService.batchCheckCart(skuIdList, userId, isChecked);
        return Result.ok();
    }

    //根据用户id,查看该用户的购物车中选中的商品列表
    @GetMapping("inner/getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable("userId") Long userId) {
        return cartService.getCartCheckedList(userId);
    }


}
